<template>
  <!-- 图层 -->
    <div id="tuceng" position="top-left" style="display: block; overflow: auto" v-if="isShow === '1'">
      <a-tree
        v-model="checkedKeys"
        checkable
        :default-expand-all="true"
        :auto-expand-parent="autoExpandParent"
        :selected-keys="selectedKeys"
        :tree-data="treeData"
        @expand="onExpand"
        @select="onSelect"
        @check="onCheck"
      />
    </div>
</template>

<script>

var tcObj = {}
var myChildren = []
export default {
    name:'tuceng',
    data(){
        return{
            //自动展开父节点--
            autoExpandParent: true,
            checkedKeys: [],
            selectedKeys: [],
            mylist: undefined,
            treeData: [
                {
                title: '图层',
                key: '图层',
                children: [],
                },
            ],
            isShow:'',
        }
    },
    props:["msg"],
    created() {
        this.$root.$on('click', (val) => {
        if (this.isShow === val) {
            this.isShow = ''
        } else {
            this.isShow = val
        }
        })

    },
    mounted(){
        if(this.msg){
        }
         
    },
    methods:{
        myunchoosed() {
            window.layers.forEach((res) => {
                res.visible = true
            })
        },
        //展开
        onExpand(expandedKeys) {
            this.expandedKeys = expandedKeys
            // this.autoExpandParent = false;
            },
        //选中
        onCheck(checkedKeys, e) {
            this.checkedKeys = checkedKeys
            this.myunchoosed()
            checkedKeys.forEach((res) => {
                window.layers.forEach((res1) => {
                if (res1.name.indexOf(res) > -1) {
                    res1.visible = false
                }
                })
            })
        },
        //点击
        onSelect(selectedKeys, info) {
            this.selectedKeys = selectedKeys
        },
    },
    watch:{
        msg(n,o){
            if(n){
            console.log(window.layers)
            window.layers.forEach((res, i) => {
                res.selectedColor=Cesium.Color.CYAN;
                
                var layername = res.name.split('@')
                res.setQueryParameter({
                            // url: 'http://10.10.12.251:8090/iserver/services/data-ZhuYaoJianZhu/rest/data',
                            url: 'http://10.10.12.251:8090/iserver/services/data-ShuangChuangChanYeYuanQu/rest/data',
                            dataSourceName: layername[1],
                            dataSetName: layername[0],
                            keyWord: 'SMID'
                        }); 

                tcObj = {
                title: layername[1],
                key: layername[1],
                children: [],
                }

                // 数组不为空
                if (myChildren.length != 0) {
                // 判断是否重复
                var temp = myChildren.findIndex((item) => item.title === layername[1])
                if (temp == -1) {
                    myChildren.push(tcObj)
                }
                } else {
                myChildren.push(tcObj)
                }
            })
            console.log(myChildren)
            window.layers.forEach((res, i) => {
                var layername1 = res.name.split('@')
                //判断是否存在
                var temp = myChildren.findIndex((item) => item.title === layername1[1])
                tcObj = {
                title: layername1[0],
                key: layername1[0],
                id: res.id,
                }
                myChildren[temp].children.push(tcObj)
            })
            this.treeData[0].children = myChildren
        
        
            }
        }
    }
    
}
</script>

<style scoped>
#tuceng {
  margin-left: 8px;
  margin-top: 8px;
  padding: 16px;
  height: 100%;
  border-radius: 5px;
  position: fixed;
  z-index: 999;
  background: white;
  opacity: 0.85;
}

</style>